//
//  TabModel.m
//  AffectionBee
//
//  Created by 程建良 on 16/8/20.
//  Copyright © 2016年 chivalrous. All rights reserved.
//

#import "TabModel.h"
#import "NetworkTool.h"
#import "ProductModel.h"
@implementation TabModel
-(instancetype)initWithDict:(NSDictionary*)dict{
    TabModel* model = [[TabModel alloc]init];
    model.name = dict[@"name"];
    model.Id = dict[@"id"];
    return model;
}
+(instancetype)tabModelWithDict:(NSDictionary*)dict{
    return [[TabModel alloc]initWithDict:dict];
}
#pragma mark --获取数据--
+(void)modelListWithUrlStr:(NSString *)urlstr parameters:(NSDictionary *)parameters SuccessBlock:(void (^)(NSArray<TabModel *> *))success failure:(void (^)(NSError *))failure{
    [[NetworkTool sharedManager]superMarketLoadDataWithUrlStr:urlstr parameters:parameters  SuccessBlock:^(id responseObject) {
        NSDictionary*dict = responseObject;
        NSDictionary*data = dict[@"data"];
        NSArray *products = data[@"product"];
        NSArray*categories = data[@"categories"];
        //分类数组
        NSMutableArray*listArray = [NSMutableArray array];
        [products enumerateObjectsUsingBlock:^(TabModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSMutableArray*tempArray = [NSMutableArray array];
            [categories enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL * _Nonnull stop) {
                TabModel *model = [TabModel tabModelWithDict:dict];
                [tempArray addObject:model];
            }];
            TabModel*model = tempArray[idx];
            //获取每类的产品数组
            NSString *key = model.Id;
            NSArray *productArray = products[idx][key];
            NSMutableArray *tempProArry = [NSMutableArray array];
            [productArray enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL * _Nonnull stop) {
                ProductModel*model = [ProductModel productModelWithDict:obj];
                [tempProArry addObject:model];
            }];
            model.products = tempProArry.copy;
            [listArray addObject:model];
        }];

        success(listArray.copy);
    } failure:failure];
}
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}
@end
